package com.vcredit.creditcat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.Ability;
import com.vcredit.creditcat.persistence.base.AbilityMapper;

/**
 * 
 **/
@Service
public class AbilityService {
	@Autowired
	private AbilityMapper mapper;

	@CacheEvict(key = "'config:Abilities'", value = "ability")
	public void insert(Ability obj) {
		mapper.insert(obj);
	}

	@CacheEvict(key = "'config:Abilities'", value = "ability")
	public void update(Ability obj) {
		mapper.update(obj);
	}

	@Cacheable(key = "'config:Abilities'", value = "ability")
	public Map<Long, Ability> findAll() {
		Map<Long, Ability> result = new HashMap<>();
		List<Ability> list = mapper.findAll();
		for (Ability ability : list) {
			result.put(ability.getAbilityId(), ability);
		}
		return result;
	}

	public Ability findById(Long abilityId) {
		Map<Long, Ability> all = findAll();
		return all.get(abilityId);
	}

	@SuppressWarnings("unused")
	private List<Ability> find(Ability obj) {
		return mapper.find(obj);
	}
}